﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace map.member
{
    public partial class i : System.Web.UI.Page
    {
        protected string item_id;
        protected string user_id;
        protected string user_recid;
        protected string item_recid;
        protected taobaouseritems useritems;

        #region 页面载入
        protected void Page_Load(object sender, EventArgs e)
        {
            bool canRead = false;
            if (Request["id"] != null && Request["id"].Trim() != string.Empty)
            {
                item_recid = Request["id"].Trim();
                useritems = taobaouseritems.Find("recid="+item_recid );
                if (useritems.Count > 0)
                {
                    user_id = useritems.taobaoUserId .Value;
                    item_id = useritems.taobaoiid.Value;
                    canRead = true;

                    InitPositionAndIntro(user_id);
                    InitShopInfo(user_id);
                    InitItemInfo(useritems);
                    //InitPositionAndIntro(user);
                }
            }

            if (ViewState["Title"] != null)
                this.Title = ViewState["Title"].ToString();

            if (!canRead)
            {
                MessageBox.ShowAndRedirect(this.Page, "对不起，您没有权限访问此页面!", ResolveUrl("~/default.aspx"));
                Response.End();
                return;
            }

            if (!IsPostBack)
            {
                InitLoginInfo();
                BindTreeView();   
            }      
        }
        #endregion

        #region 初始化方法

        #region 绑定分类相关
        protected void BindTreeView()
        {
            this.TreeView1.Nodes.Clear();
            TreeNode rootNode = new TreeNode("所有宝贝", "0");
            this.TreeView1.Nodes.Add(rootNode);
            if (  user_id != null)
            {
                taobaoItemCat itemcate = taobaoItemCat.Find("taobaouserid='" + user_id + "'", " taobaosort_order desc");
                if (itemcate.Count > 0)
                {
                    AddTreeNode(rootNode, "0", itemcate);
                }
            }
        }

        protected void AddTreeNode(TreeNode parentNode, string parentcateid, taobaoItemCat itemcate)
        {
            if (parentcateid != string.Empty && parentcateid != null && user_id != null && itemcate.Count > 0)
            {
                DataRow[] dRows = itemcate.Select("taobaoparentcid='" + parentcateid + "'", " taobaosort_order desc");
                foreach (DataRow dRow in dRows)
                {
                    TreeNode childNode = new TreeNode(dRow["taobaoname"].ToString(), dRow["taobaocid"].ToString());
                    parentNode.ChildNodes.Add(childNode);
                    AddTreeNode(childNode, dRow["taobaocid"].ToString(), itemcate);
                }
            }
        }
        #endregion

        protected void InitPositionAndIntro(string userid)
        {
            cbuser user = cbuser.Find("cbuserid='"+userid +"'");
            if (user.Count > 0)
            {
                user_recid = user.RECID.ToString();

                Label_nick.Text = user.cbusernickname.Value;
                Label_bad.Text = user.pingjia_bad.Value.ToString();
                Label_good.Text = user.pingjia_good.Value.ToString();
                Label_area.Text = user.省编码.Value;
                Literal_chat.Text = cbuser.getChatStrfromuserid(user.cbuserid.Value);
                cbBroker b = cbBroker.Find(" ShopOwnerID = '" + user.cbuserid.Value
                    + "' and StartDate <= '" + DateTime.Today.ToShortDateString() + "'");
                if (b.Count > 0)
                {
                    Literal_jjr.Text = cbuser.getChatStrfromuserid(b.BrokerID.ToString());
                }

                //当前店铺链接
                currentShopAnchor.HRef = ResolveUrl("~/s.aspx?id=" + user.RECID .ToString ());
            }
        }

        protected void InitShopInfo(string userid)
        {
            cbshop shop = cbshop.Find("cbuserid='"+userid +"'");
            if (shop.Count > 0)
            {
                //当前店铺链接
                currentShopAnchor.InnerHtml = shop.cbshoptitle.Value;

                LogoImage.Src = shop.cbpathlogo.Value;
                LogoImage.Src = (LogoImage.Src.Trim() == string.Empty) ? ResolveUrl("~/img/userdefaultimg.gif") : LogoImage.Src;



                ShopTitleAnchor.InnerHtml = shop.cbshoptitle.Value;
                ShopTitleAnchor.HRef = ResolveUrl("~/s.aspx?id=" + user_recid);

                IntroLiteral.Text = Server.HtmlDecode(shop.shopgonggao.Value);

                if (shop.NoYes_sell.Value == RuleCode.Enum.NoYes.Yes)
                {          
                    OrderButton.Enabled = true;
                    OrderButton.CssClass = "canOrderButton";
                    OrderButton.Text = " ";
                    OrderButton.ToolTip  ="欢迎订购";
                    Label_Item.Text = "欢迎购物"; 
                }
                else
                {
                    OrderButton.Enabled = false;
                    OrderButton.CssClass = "orderButton";
                    OrderButton.Text = "购买";
                    OrderButton.ToolTip = "商家未开通网购功能,暂时未能购买";
                    Label_Item.Text = "商家未开通网购功能";
                }


                //Css样式和Banner图片
                string cssFileName = (shop.cbshopcssfilename.Value.Trim() == string.Empty) ? "s_i" : shop.cbshopcssfilename.Value;
                if (Request["css"] != null)
                    cssFileName = Request["css"];
                ViewState["css"] = cssFileName;
                RegisterCssFile(cssFileName);

                string itemBannerPic = shop.cbitembannerpic .Value;
                if (Request["itembanner"] != null)
                    itemBannerPic = Request["itembanner"];
                if (itemBannerPic.Trim() != string.Empty)
                {
                    ViewState["itembanner"] = itemBannerPic;
                    BannerDiv.Style["background-image"] = "url(" + ViewState["itembanner"].ToString() + ")";
                }   
            }
        }

        protected void InitItemInfo(taobaouseritems item)
        {
            if (item.Count > 0)
            {           
                currentItemAnchor.InnerHtml = item.taobaotitle .Value ;
                this.Title = item.taobaotitle.Value;
                ViewState["Title"] = this.Title;

                ItemTitleLabel.Text = item.taobaotitle.ToString();
                ItemImage.Src  = item.taobaopic_path.ToString();
                ItemImage.Src = (ItemImage.Src.Trim() == string.Empty) ? ResolveUrl("~/img/userdefaultimg.gif") : ItemImage.Src;

                Label_price.Text = item.taobaoprice.ToString() + " 元";
                if (item.taobaofreight_payer.ToString() == "seller")
                {
                    Label_kd.Text = "卖家承担运费";
                    Label_py.Text = "卖家承担运费";
                    Label_ems.Text = "卖家承担运费";
                    Label_sh.Text = "卖家承担运费";
                }
                else
                {
                    Label_kd.Text = item.taobaopost_fee.ToString()+" 元";
                    Label_py.Text = item.taobaoexpress_fee.ToString() + " 元";
                    Label_ems.Text = item.taobaoems_fee.ToString() + " 元";
                    Label_sh.Text =( (item.kuaidi.ToString().Trim() == string.Empty) ? "0.0" : item.kuaidi.ToString() )+ " 元";
                }  
                DetailInfoLiteral.Text = item.taobaodesc.ToString();                 
            }
        }

        /// <summary>
        /// 初始化 登录信息
        /// </summary>
        protected void InitLoginInfo()
        {
            if (this.Request.Url.AbsolutePath != ResolveUrl("~/member/login.aspx")
                    && this.Request.Url.AbsolutePath != ResolveUrl("~/member/regist1.aspx"))
            {
                if (AliApi.appUserId == "")
                {
                    HyperLink_login.Text = "登录";
                    HyperLink_login.NavigateUrl = ResolveUrl("~/member/login.aspx") + "?redirecturl=" + this.Request.Url;

                    HyperLink_logout.NavigateUrl = ResolveUrl("~/member/regist1.aspx") + "?redirecturl=" + this.Request.Url;
                    HyperLink_logout.Text = "注册";
                }
                else
                {
                    HyperLink_login.Text = AliApi.appNickName;
                    HyperLink_login.NavigateUrl = "#";
                    HyperLink_logout.NavigateUrl = ResolveUrl("~/member/loginout.aspx") + "?redirecturl=" + this.Request.Url;
                    HyperLink_logout.Text = "注销";
                }
            }
            else
            {
                HyperLink_login.Text = "登录";
                HyperLink_login.NavigateUrl = ResolveUrl("~/member/login.aspx");

                HyperLink_logout.NavigateUrl = ResolveUrl("~/member/regist1.aspx");
                HyperLink_logout.Text = "注册";
            }
        }
        #endregion

        #region 事件
        protected void OrderButton_Click(object sender, EventArgs e)
        {
            if (useritems.Count > 0 && item_id != null)
            {
                if (useritems.taobaoUserId.ToString() == AliApi.appUserId)
                {
                    MessageBox.Show(this.Page, "不能购买自己的商品!");
                    return;
                }
                if (AliApi.appUserId == string.Empty )
                {
                    string loginPageUrl = ResolveUrl("~/member/login.aspx") + "?redirecturl=" + ResolveUrl("~/member/i.aspx?id=" + item_recid);
                    Response.Redirect(loginPageUrl);
                    return;
                }
                else
                {
                    string orderPageUrl = ResolveUrl("~/member/order/NewOrder.aspx?ID=" + item_id);
                    Response.Redirect(orderPageUrl );
                    return;
                }
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (user_id != null)
            {
                cbuser user = cbuser.Find("cbuserid='"+user_id +"'");
                if (user.Count > 0)
                {
                    string cataListStr = string.Empty;
                    TreeNode currentNode = TreeView1.SelectedNode;
                    Session["nodevalue"] = currentNode.Value;
                    //cataListStr = currentNode.Value;
                    //while (currentNode.Parent != null)
                    //{
                    //    currentNode = currentNode.Parent;
                    //    cataListStr = currentNode.Value + "\\" + cataListStr;
                    //}
                    //cataListStr = "s" + cataListStr;
                    //Session["nodevalue"] = cataListStr;
                    Response.Redirect(ResolveUrl("~/s.aspx?id=" + user.RECID.ToString ()));                    
                }
            }
           
        }
        #endregion

        #region css相关
        protected void RegisterCssFile(string cssFileName)
        {
            HtmlLink cssLink = new HtmlLink();
            cssLink.Attributes["type"] = "text/css";
            cssLink.Attributes["rel"] = "stylesheet";
            cssLink.Attributes["href"] = ResolveUrl("~/css/s_i/" + cssFileName + ".css");
            Page.Header.Controls.Add(cssLink);
        }
        #endregion
    }
}
